package com.androidcourse.changeandget;

import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;

import android.app.FragmentTransaction;

import android.app.FragmentManager;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.androidcourse.changeandget.Adapt.GoodAdapt;
import com.androidcourse.changeandget.Database.DBHelper;
import com.androidcourse.changeandget.User.Good;
import com.androidcourse.changeandget.User.User;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class homepage extends AppCompatActivity implements View.OnClickListener {

    private String SID;
    private String Password;
    private TextView stu_id;
    private TextView Sname;
    private TextView Sestate;
    private ListView Goodlist;
    private Button Renew;
    private User curperson;
    private GoodAdapt goodAdapt;
    private int mCurrentFragPosition;

    private Fragment Goods = new Goods();
    private Fragment Person = new Person();
    private FragmentManager fragmentManager;

    private LinearLayout linearLayout1;
    private LinearLayout linearLayout2;

    private DBHelper mSQlite;
    List<Good> Good = new LinkedList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_homepage);
        mSQlite = new DBHelper(homepage.this);

        if (curperson==null){
            curperson=curuser();
        }
        linearLayout1 = findViewById(R.id.LinearLayout1);
        linearLayout2 = findViewById(R.id.LinearLayout2);
        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        initFragment();//fragment布局
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initFragment();
    }

    protected void onSaveInstanceState(Bundle outState) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.remove(Goods);
        transaction.remove(Person);
        transaction.commitAllowingStateLoss();
        super.onSaveInstanceState(outState);
    }//在打开新的activity后移除以及打开的fragment布局


    private void initFragment() {
        fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.id_content, Goods);
        transaction.add(R.id.id_content, Person);
        transaction.commit();
        hideFragment(transaction);
    }

    private void hideFragment(FragmentTransaction transaction) {
        transaction.hide(Goods);
        transaction.hide(Person);
    }

    private void setbackgroundcolor(View v) {
        switch (v.getId()) {
            case R.id.LinearLayout1:
                linearLayout1.setBackgroundColor(Color.parseColor("#0DDDDB"));
                break;
            case R.id.LinearLayout2:
                linearLayout2.setBackgroundColor(Color.parseColor("#0DDDDB"));
                break;
            default:
                break;
        }
    }

    private void returnbackground(View v) {
        switch (v.getId()) {
            case R.id.LinearLayout1:
                linearLayout1.setBackgroundColor(Color.parseColor("#269C92"));
                break;
            case R.id.LinearLayout2:
                linearLayout2.setBackgroundColor(Color.parseColor("#269C92"));
                break;
            default:
                break;
        }
    }


    private void showfragment(int i) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                transaction.show(Goods);
                setbackgroundcolor(linearLayout1);
                returnbackground(linearLayout2);
                break;
            case 1:
                transaction.show(Person);
                setbackgroundcolor(linearLayout2);
                returnbackground(linearLayout1);
                break;
            default:
                break;
        }
        transaction.commit();
        mCurrentFragPosition = i;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.LinearLayout1: {

                showfragment(0);
                showGoodInformation();
                jumptosubmit();
                jumptoexchange();
            }
            break;
            case R.id.LinearLayout2: {
                showfragment(1);
                showPerson();       //显示个人信息界面的信息函数
                System.out.println(curperson.getUsername());
                Renew();
                jumptopersonalinfo();
            }
            break;
            default:
                break;
        }
    }

    public void showPerson() {
        TextView stu_id = (TextView) findViewById(R.id.stu_id);
        TextView stu_name = (TextView) findViewById(R.id.stu_name);
        TextView stu_house = (TextView) findViewById(R.id.stu_house);
        TextView stu_tele = (TextView)findViewById(R.id.stu_tele);
        stu_id.setText(curperson.getId());
        stu_name.setText(curperson.getUsername());
        stu_house.setText(curperson.getSestate());
        stu_tele.setText(curperson.getStele());
    }

    public void jumptosubmit() {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        ImageButton button1 = (ImageButton) findViewById(R.id.ImageButton1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(homepage.this, submit.class);

                Bundle bundle=new Bundle();
                bundle.putString("person",curperson.getId());
                intent = intent.putExtras(bundle);//传入信息
                startActivity(intent);
                //finish();
            }
        });

    }

    public void jumptopersonalinfo() {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        ImageButton button = (ImageButton) findViewById(R.id.imageButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent3 = new Intent();
                intent3.setClass(homepage.this, PersonalInfo.class);
                Bundle bundle3 = new Bundle();
                bundle3.putString("person", curperson.getId());
                intent3 = intent3.putExtras(bundle3);
                startActivity(intent3);
                //finish();
            }
        });
    }

    public void showGoodInformation() {

        goodAdapt = new GoodAdapt(getApplicationContext());
        Goodlist = (ListView) findViewById(R.id.goodlist);
        Good = mSQlite.readGood();
        goodAdapt.setData(Good);
        Goodlist.setAdapter(goodAdapt);
    }

    public User curuser() {
        User person;
        //从Intent中取出Bundle
        Bundle bundle = this.getIntent().getExtras();
        assert bundle != null;
        String SID=bundle.getString("thisuser");

        ArrayList<User> data = mSQlite.getAllDATA();
        User thisuser=new User();//创建当前用户
        for (int i = 0; i < data.size(); i++) {
            User user = data.get(i);   //可存储账号数量
            if (SID.equals(user.getId()) ) {//判断数据库中是否有相同数据
                thisuser.setId(user.getId());//如果账号正确则把数据传入当前用户
                thisuser.setUserpwd(user.getUserpwd());
                thisuser.setSestate(user.getSestate());
                thisuser.setSsex(user.getSsex());
                thisuser.setStele(user.getStele());
                thisuser.setUsername(user.getUsername());
                break;
            }
        }
        return thisuser;
    }

    public void jumptoexchange(){
        Goodlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Good commodity = (Good) Goodlist.getAdapter().getItem(position);//找到当前行的商品信息
                Bundle bundle1 = new Bundle();
                bundle1.putString("Gname",commodity.getGname());
                bundle1.putString("Time",commodity.getHood());
                bundle1.putString("Sestate",curperson.getSestate());
                bundle1.putString("DGood",commodity.getDGood());
                bundle1.putString("EGood",commodity.getEGood());
                bundle1.putString("GID",commodity.getGID());
                bundle1.putString("SID",curperson.getId());
                //bundle1.putByteArray("picture",commodity.getImage());
                Intent intent = new Intent(homepage.this, exchange.class);
                intent.putExtras(bundle1);
                startActivity(intent);
            }
        });

    }

    void Renew(){
        Renew=(Button) findViewById(R.id.renew);
        TextView stu_id = (TextView) findViewById(R.id.stu_id);
        TextView stu_name = (TextView) findViewById(R.id.stu_name);
        TextView stu_house = (TextView) findViewById(R.id.stu_house);
        TextView stu_tele = (TextView)findViewById(R.id.stu_tele);

        Renew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ArrayList<User> data = mSQlite.getAllDATA();
                User thisuser=new User();//创建当前用户
                for (int i = 0; i < data.size(); i++) {
                    User user = data.get(i);   //可存储账号数量
                    if (curperson.getId().equals(user.getId()) ) {//判断数据库中是否有相同数据
                        thisuser.setId(user.getId());//如果账号正确则把数据传入当前用户
                        thisuser.setUserpwd(user.getUserpwd());
                        thisuser.setSestate(user.getSestate());
                        thisuser.setSsex(user.getSsex());
                        thisuser.setStele(user.getStele());
                        thisuser.setUsername(user.getUsername());
                        break;
                    }
                }
                stu_id.setText(thisuser.getId());
                stu_name.setText(thisuser.getUsername());
                stu_house.setText(thisuser.getSestate());
                stu_tele.setText(thisuser.getStele());
            }
        });
    }
}